package site.assad.dynamicProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * Author: Al-assad 余林颖
 * E-mail: yulinying_1994@outlook.com
 * Date: 2017/12/4 23:56
 * Description: 基于JDK动态代理：FooServcieImpl 中提取出来的横切逻辑代码的增强类
 */
public class PerformanceHandler implements InvocationHandler{

    private Object target;   //储存目标业务类 FooServiceImpl

    public PerformanceHandler(Object target) {
        this.target = target;
    }

    //设置切点和增强代码
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        PerformanceMonitor.begin(target.getClass().getName()+"."+method.getName());  //开始测试
        Object result = method.invoke(target,args);    //通过反射方法调用目标业务类的目标方法
        PerformanceMonitor.end();     //结束测试
        return result;
    }
}
